/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddItemDialog.java
 *
 * Created on Apr 7, 2011, 11:07:16 PM
 */

package client;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import org.workplicity.inventorycontrol.entry.Inventory;
import org.workplicity.inventorycontrol.entry.Item;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

/**
 *
 * @author Neal
 * @author Brian Gormanly
 */
public class AddItemDialog extends javax.swing.JDialog
{
    private boolean addedItem = false;
    
    private Item thisItem;
    private Inventory currentInventory;

    /** Creates new form AddItemDialog */
    public AddItemDialog(java.awt.Frame parent, Inventory inventory, Item item, boolean modal)
    {
        super(parent, modal);

        // Attempt to set the appearance to the system default
        try
        {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
            currentInventory = inventory;
            thisItem = item;
            
        }
        catch (Exception ex)
        {
            System.out.println("Failed to set the look and feel.");
        }

        initComponents();
        init();
    }

    private void init()
    {
        // Get the screen size object
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();

        // Get the size of the screen
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;

        // Get the size of the current window
        int windowHeight = this.getHeight();
        int windowWidth = this.getWidth();

        int newX = (screenWidth / 2) - (windowWidth / 2);
        int newY = (screenHeight / 2) - (windowHeight / 2);
        this.setLocation(newX, newY);
    }

    public boolean addedItem()
    {
        return this.addedItem;
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        itemNameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        itemModelTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        itemDescriptionTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        itemOEMTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        reorderThresholdTextField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(client.MainFrame.class).getContext().getResourceMap(AddItemDialog.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        itemNameTextField.setText(resourceMap.getString("itemNameTextField.text")); // NOI18N
        itemNameTextField.setName("itemNameTextField"); // NOI18N
        itemNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemNameTextFieldActionPerformed(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        itemModelTextField.setText(resourceMap.getString("itemModelTextField.text")); // NOI18N
        itemModelTextField.setName("itemModelTextField"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        itemDescriptionTextField.setText(resourceMap.getString("itemDescriptionTextField.text")); // NOI18N
        itemDescriptionTextField.setName("itemDescriptionTextField"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        itemOEMTextField.setText(resourceMap.getString("itemOEMTextField.text")); // NOI18N
        itemOEMTextField.setName("itemOEMTextField"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        reorderThresholdTextField.setText(resourceMap.getString("reorderThresholdTextField.text")); // NOI18N
        reorderThresholdTextField.setName("reorderThresholdTextField"); // NOI18N

        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(itemOEMTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(itemDescriptionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(itemModelTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(itemNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(reorderThresholdTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)))
                    .addComponent(saveButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(itemNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemModelTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemDescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemOEMTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(reorderThresholdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        String errorMessage = "";
        
        String nameEntered = itemNameTextField.getText();
        String descriptionEntered = itemDescriptionTextField.getText();
        String modelNumber = itemModelTextField.getText();
        String oemNumber = itemOEMTextField.getText();
        Integer reorderThres;
        
        try {
             reorderThres = Integer.parseInt(reorderThresholdTextField.getText());
        }
        catch(Exception e) {
            reorderThres = 1;
            errorMessage = "Non numeric value entered for order threshold, Value much be 1 or greater.\n";
        }
        
        if(nameEntered.equals("") || nameEntered.equals("Item name")) {
            errorMessage += "Plaese enter a valid name\n";
        }
        if(descriptionEntered.equals("") || descriptionEntered.equals("Item description")) {
            errorMessage += "Please enter a Description\n";
        }
        if(modelNumber.equals("") || modelNumber.equals("Item model")) {
            errorMessage += "Plaese enter a valid model\n";
        }
        if(oemNumber.equals("") || oemNumber.equals("Item OEM")) {
            errorMessage += "Plaese enter a valid OEM\n";
        }
        if(reorderThres < 1) {
            errorMessage += "Plaese enter a valid re-order threshold larger then 0\n";
        }
        
        
        if(errorMessage.length() > 0) {
            JOptionPane.showMessageDialog(this,
                        errorMessage,
                        "Required parameters missing",
                        JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            try {
                Item thisItem = new Item(nameEntered);

                thisItem.setName(nameEntered);
                thisItem.setDescription(descriptionEntered);
                thisItem.setInventoryId(currentInventory.getId());
                thisItem.setModelNumber(modelNumber);
                thisItem.setOem(oemNumber);
                thisItem.setStockThreshold(reorderThres);

                currentInventory.insert(thisItem);


                System.out.println(thisItem.getId());

                WorkletContext context = WorkletContext.getInstance();

                if (!Helper.insert(thisItem, "Inventories", context)) {
                    System.out.println("insert Item into Inventory failed!");
                }

                System.out.println(thisItem.getId());
            }
            catch(Exception e) {
                JOptionPane.showMessageDialog(this, "Error saving record",
                        "Item save failed", JOptionPane.ERROR_MESSAGE);
            }

            this.setVisible(false);
            this.addedItem = true;
            
            dispose();
        }
}//GEN-LAST:event_saveButtonActionPerformed

    private void insertItem(Item item){
        
        
        
        final AddItemDialog frame = this;
        WorkletContext context = WorkletContext.getInstance();

         System.out.println(item.getId());
         
         if (!Helper.insert(item, "Inventories", context)) {
                        System.out.println("insert Item into Inventory failed!");
                    }
         
         System.out.println(item.getId());

        // System.out.println(inventory.getId());

    }// end insert
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
        this.addedItem = false;
}//GEN-LAST:event_cancelButtonActionPerformed

    private void itemNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemNameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_itemNameTextFieldActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField itemDescriptionTextField;
    private javax.swing.JTextField itemModelTextField;
    private javax.swing.JTextField itemNameTextField;
    private javax.swing.JTextField itemOEMTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField reorderThresholdTextField;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
